using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Imagination.Physics
{
    /// <summary>
    /// IPhysicsService represents the class that implements the 
    /// physics model in the simulation. It does the actual calculations
    /// and moves things around the scene based on them.
    /// </summary>
    public abstract class PhysicsService
    {
        protected Dictionary<Type, object> constants;
        protected Game game;

        protected int counterThingsMoved;

        protected PhysicsService(Game game)
        {
            this.game = game;
            constants = new Dictionary<Type, object>();
        }

        public int CounterThingsMoved
        {
            get { return counterThingsMoved; }
        }

        public void BeginPhysics()
        {
            counterThingsMoved = 0;
        }

        public abstract void Calculate(GameTime gameTime, Thing thing);
        public abstract void InitializePhysics(Thing thing);

        private Dictionary<string, T> GetConstants<T>()
        {
            Dictionary<string, T> tConstants = null;
            if (constants.ContainsKey(typeof(T)))
            {
                tConstants = constants[typeof(T)] as Dictionary<string, T>;
            }
            else
            {
                tConstants = new Dictionary<string, T>();
                constants[typeof(T)] = tConstants;
            }
            return tConstants;
        }

        public void SetConstant<T>(string name, T constant)
        {
            Dictionary<string, T> tConstants = GetConstants<T>();
            tConstants[name] = constant;
        }

        public T GetConstant<T>(string name)
        {
            Dictionary<string, T> tConstants = GetConstants<T>();
            return tConstants[name];
        }
    }
}
